﻿using System.Text.RegularExpressions;
using System.Web;
using Als.Common.String;

namespace Als.Common.Net
{
    public static  class QueryString
    {

        #region 私有方法
        /// <summary>
        /// HttpContext Current
        /// </summary>
        private static HttpContext Current => HttpContext.Current;

        /// <summary>
        /// HttpContext Request
        /// </summary>
        private static HttpRequest Request => Current.Request;

        /// <summary>
        ///  HttpContext Response
        /// </summary>
        private static HttpResponse Response => Current.Response;

        #endregion

        #region 共有方法

        /// <summary>
        /// Request.Params
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string P(this string name)
        {
            return Request.Params[name] == null ? "" : Request.Params[name];
        }

        /// <summary>
        /// Request.QueryString
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string Q(this string name)
        {
            return Request.QueryString[name] == null ? "" : Request.QueryString[name];
        }

        /// <summary>
        /// Request.Form
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string F(this string name)
        {
            return Request.Form[name] == null ? "" : Request.Form[name];
        }

        /// <summary>
        /// 获取url中的id
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static int QId(this string name)
        {
            return name.StrToId();
        }

        /// <summary>
        /// 获取正确的id
        /// </summary>
        /// <param name="value"></param>
        /// <returns>返回正确的整数ID，失败返回0</returns>
        public static int StrToId(this string value)
        {
            return value.IsNum() ? int.Parse(value) : 0;
        }

        #endregion

    }
}
